using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

// 排行榜
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class RankListController : ControllerBase
    {
        //阅读量表
        private IRepository<ReadQuantities> _reqRepository;

        //评论表
        private IRepository<Comments> _comRepository;

        // 文章表
        private IRepository<Articles> _articlesRepository;

        public RankListController(
            IRepository<ReadQuantities> reqRepository,
            IRepository<Comments> comRepository,
            IRepository<Articles> articlesRepository
        )
        {
            _reqRepository = reqRepository;
            _comRepository = comRepository;
            _articlesRepository = articlesRepository;
        }

        //48小时阅读排行
        [HttpGet]
        public dynamic FortyEight()
        {

            //文章表
            var articles = _articlesRepository.Table.Where(o=>o.IsEnable==true && o.IsDelete==false).ToList();

            Console.WriteLine(DateTime.Now);

            //新建List空数组
            ArrayList arraylist = new ArrayList();

            //判断文章创建至现在是否超过48小时
            articles
                .ForEach(item =>
                {
                    //截止至现在使时间
                    var endTime = DateTime.Now;

                    //开始于文章创建时间
                    var starTime = item.CreatedTime;
                    Console.WriteLine(starTime);

                    //保存时间差
                    TimeSpan t1 = endTime - starTime;
                    Console.WriteLine(t1);

                    //判断是否超过48小时
                    if (t1.TotalHours > 48)
                    {
                        Console.WriteLine("超过48");
                    }
                    else
                    {
                        arraylist.Add(item);
                    }
                });
                
            return JsonHelper
                .Serialize(new
                {
                    Code = 200,
                    Data = arraylist,
                    Msg = "获取成功"
                });
        }

        //添加阅读量表数据进行测试
        // ReadQuantitie
        [HttpPost]
        public dynamic AddRead(ReadQuantitie model)
        {
            var articleId = model.ArticleId;
            var userId = model.UserId;
            var read =
                new ReadQuantities
                {
                    ArticleId = articleId,
                    UserId = userId
                };

            _reqRepository.Insert(read);

            return JsonHelper
                .Serialize(new { Code = 200, Data = read, Msg = "获取成功" });
        }


        //十天编辑推荐
        [HttpGet, Route("tenedit")]
        public dynamic TenDaysEdit()
        {
            //文章表
            var articles = _articlesRepository.Table.Where(o=>o.IsEnable==true && o.IsDelete==false).ToList();

            //新建List空数组
            ArrayList arraylist = new ArrayList();

            //判断文章创建至现在是否超过10天
            articles
                .ForEach(item =>
                {

                    //截止至现在使时间
                    var endTime = DateTime.Now;

                    //开始于文章创建时间
                    var starTime = item.CreatedTime;


                    //保存时间差
                    TimeSpan t1 = endTime - starTime;

                    //判断是否超过10天
                    if (t1.TotalDays > 10)
                    {
                        Console.WriteLine("超过10天");
                    }
                    else
                    {
                        //判断是否被编辑推荐
                        if (item.IsRecommend == true)
                        {
                            arraylist.Add(item);
                        }
                        else
                        {
                            Console.WriteLine("未被编辑推荐");
                        }

                    }
                });

            return JsonHelper
            .Serialize(new
            {
                Code = 200,
                Data = arraylist,
                Msg = "获取成功tenedit"
            });
        }

        //十天评论排行
        [HttpGet, Route("tencom")]
        public dynamic TenDaysCom()
        {

            //文章表
            var articles = _articlesRepository.Table.Where(o=>o.IsEnable==true && o.IsDelete==false).ToList();

            //新数组List类型
            ArrayList arraylist = new ArrayList();

            //判断文章创建至现在是否超过10天
            articles.ForEach(item =>
            {
                //截止至现在使时间
                var endTime = DateTime.Now;

                //开始于文章创建时间
                var starTime = item.CreatedTime;

                //保存时间差
                TimeSpan t1 = endTime - starTime;

                //判断是否超过10天
                if (t1.TotalDays > 10)
                {
                    Console.WriteLine("超过10天");
                }
                else
                {

                    arraylist.Add(item);
                }
            });

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = arraylist,
                Msg = "获取成功tencom"
            });
        }
    }
}
